package controllers;

import models.*;
import play.mvc.Controller;

import java.util.List;

/**
 * @author Javier Gutierrez <bichocj@gmail.com>
 */
public class EventPageController extends Controller {

    /*
     * Show the main page for current event
     * @nick Event's nick
     */
    public static void show(String nick) {
        //TODO validate when event dosen't exists
        Event event = Event.getByNick(nick);
        render(event);
    }


    public static void edit(String nick) {
        //TODO validate when event dosen't exists
        Event event = Event.getByNick(nick);
        render(event);
    }

    /*
     * Show the form to register to the current eevent
     * @nick Event's nick
     */
    public static void form(String nick) {
        Event event = Event.getByNick(nick);
        List<DocumentType> documentsType = DocumentType.findAll();
        render(event, documentsType);
    }

    public static void form(String nick, String documentType, String code, Person person) {
        Event event = Event.getByNick(nick);
        List<DocumentType> documentsType = DocumentType.findAll();
        render(event, documentType, code, person);
    }

    /*
     * Recorded in the current event
     * @nick Event's nick
     * @name Person's name
     * @username Person's username
     * @password Person's pasword
     */
    public static void register(String nick, String documentType, String code, Person person) {

        boolean haveError = false;
        Long idDocumentType = Long.valueOf(documentType);
        String errorField = "";
        //Validate params
        if (documentType.isEmpty()) {
            errorField += "El tipo de documento no es valido. ";
            haveError = true;
        }
        if (code.isEmpty()) {
            errorField += "El codigo del documento no es valido. ";
            haveError = true;
        }
        if (person == null) {
            errorField += "Los datos de la persona no son validos. ";
            haveError = true;
        }
        if (person.name.isEmpty()) {
            errorField += "El nombre de la persona no es valido. ";
            haveError = true;
        }
        if (person.email.isEmpty()) {
            errorField += "El email de la persona no es valido. ";
            haveError = true;
        }
        if (person.surname.isEmpty()) {
            errorField += "El apellido de la persona no es valido.";
            haveError = true;
        }
        //redirect if any param isn't valid
        if (haveError) {
            flash.error(errorField);
            form(nick, documentType, code, person);
        }

        //get a person by same email and update the current person
        Person personInDB = Person.find("byEmail", person.email).first();
        if (personInDB != null) {
            person = personInDB;
        } else {
            person.isActive = true;
            person = person.save();
        }

        //TODO query for a PersonDocument where documentType, person and code are the same
        PersonDocument personDocument = new PersonDocument();
        personDocument.documentType = DocumentType.findById(idDocumentType);
        personDocument.person = person;
        personDocument.code = code;
        personDocument.save();


        Event event = Event.getByNick(nick);
        Inscription inscription = new Inscription();
        inscription.isActive = true;
        inscription.event = event;
        inscription.personDocument = personDocument;
        inscription.profile = Profile.getAsistentProfile();


        inscription.save();

        String message = "se registro a " + person;
//        render(event, message);
        flash.success("Se registro a la persona");
        form(nick);
    }

    public static void showPresentations(String nick) {
        Event event = Event.getByNick(nick);
        render(event);
    }

    public static void saveIndexContent(String nick, String indexContent) {
        Event event = Event.getByNick(nick);
        event.indexContent = indexContent;
        Event.reloadByNick(nick);
        redirect("/" + event.nick);
    }
}
